<script setup lang="ts">
	import { ref } from 'vue'
	import { postLogin,postThirdLogin } from '@/api/user'
	import { onLoad } from "@dcloudio/uni-app";
	const form = ref({
		LoginId: '',
		Password: '',
	})
	let Code = ''
	// #ifdef  MP-WEIXIN
	onLoad(async () => {
		const res = await wx.login();
		Code = res.code;
	});
	// #endif
	const onGetphonenumber = async (ev : any) => {
		if (ev.detail.errMsg === 'getPhoneNumber:ok') {
			console.log(ev);
			console.log('微信登陆')
			const EncryptedData = ev.detail.encryptedData!
			const Iv = ev.detail.iv!
			const res=await postThirdLogin({ Code, EncryptedData, Iv })
			uni.setStorageSync('refreshToken', res.data.refreshToken)
			uni.setStorageSync('accessToken', res.data.accessToken)
			uni.showToast({ icon: 'none', title: '登录成功' })
			setTimeout(() => {
				console.log('跳转')
				uni.switchTab({ url: '/pages/user/index' })
			}, 1000)
		} else {
			console.log('用户拒绝授权');
		}
		
	}

	const onGetphonenumberSimper = async () => {
		const res = await postLogin(form.value)
		console.log(res)
		uni.setStorageSync('refreshToken', res.data.refreshToken)
		uni.setStorageSync('accessToken', res.data.accessToken)
		uni.showToast({ icon: 'none', title: '登录成功' })
		setTimeout(() => {
			console.log('跳转')
			uni.navigateBack()
			// uni.switchTab({ url: '/pages/user/index' })
		}, 1000)
	}
</script>

<template>
	<view class="viewport">
		<div class="logo">Lucent</div>
		<view class="login">
			<view class="form">
				<view class="form-content">
					<view class="form-item">
						<text class="label">
							<text class="icon icon-youxiang"></text>
						</text>
						<input class="input" v-model="form.LoginId" type="text" placeholder="请输入账号或邮箱" />
					</view>
					<view class="form-item">
						<text class="label">
							<text class="icon icon-tianchongxing-1"></text>
						</text>
						<input class="input" v-model="form.Password" type="password" placeholder="请输入密码" />
					</view>
				</view>
				<view class="tip">仅限企业客户使用</view>
				<button class="form-button" @tap="onGetphonenumberSimper">登陆</button>

				<view class="caption">
					<text>其他登陆方式</text>
				</view>
				<view class="options">
					<button  open-type="getPhoneNumber" @getphonenumber="onGetphonenumber">
						一键登录
					</button>
				</view>
			</view>
			<view class="tips"></view>
		</view>
	</view>
</template>

<style lang="scss" scoped>
	page {
		height: 100%;
	}

	.viewport {
		display: flex;
		flex-direction: column;
		align-items: center;
		height: 100%;
		padding: 20rpx 40rpx;
	}

	.logo {
		font-size: 60rpx;
		padding: 100rpx 40rpx;
		color: #9ca3af;
	}

	.form {
		width: 90vw;
		// background-color: #f4f4f4;

		&-content {
			padding: 0 20rpx;
			border-radius: 10rpx;
		}

		&-item {
			display: flex;
			height: 96rpx;
			line-height: 46rpx;
			padding: 25rpx 10rpx;
			font-size: 28rpx;
			border: 1px solid #e1e1e1;
			margin-bottom: 32rpx;
			border-radius: 10rpx;

			.label {
				width: 100rpx;
				color: #333;

				.icon {
					padding-left: 20rpx;
					font-size: 40rpx;
					color: #999;
				}
			}

			.account {
				color: #666;
			}

			.input {
				flex: 1;
				display: block;
				height: 46rpx;
			}

			.radio {
				margin-right: 20rpx;
			}

			.picker {
				flex: 1;
			}

			.placeholder {
				color: #808080;
			}
		}

		&-button {
			height: 80rpx;
			text-align: center;
			line-height: 80rpx;
			margin: 30rpx 20rpx;
			color: #fff;
			border-radius: 10rpx;
			font-size: 30rpx;
			background-color: $uni-color-primary;
		}

		.tip {
			height: 80rpx;
			text-align: center;
			line-height: 80rpx;
			margin: 30rpx 20rpx;
			font-size: 30rpx;
			color: #6b7280;
		}
	}

	.tips {
		position: absolute;
		bottom: 80rpx;
		left: 20rpx;
		right: 20rpx;
		font-size: 22rpx;
		color: #999;
		text-align: center;
	}

	.caption {
		margin-top: 100rpx;
		// width: 440rpx;
		line-height: 1;
		border-top: 1rpx solid #ddd;
		font-size: 26rpx;
		color: #999;
		position: relative;

		text {
			transform: translate(-40%);
			background-color: #fff;
			position: absolute;
			top: -12rpx;
			left: 50%;
		}
	}

	.options {
		display: flex;
		justify-content: center;
		align-items: center;
		margin-top: 70rpx;

		button {
			padding: 5rpx 50rpx;
			border-radius: 10rpx;
			font-size: 26rpx;
			border: 1rpx solid #444;
		}
	}

	.icon-weixin {
		font-size: 24rpx;
		color: #444;
		display: flex;
		flex-direction: column;
		align-items: center;

		&::before {
			display: flex;
			align-items: center;
			justify-content: center;
			width: 80rpx;
			height: 80rpx;
			margin-bottom: 6rpx;
			font-size: 40rpx;
			border: 1rpx solid #444;
			border-radius: 50%;
		}
	}

	.icon-weixin::before {
		border-color: $uni-color-primary;
		color: $uni-color-primary;
	}
</style>